package org.jeecg.modules.weixin.service.impl;

import org.jeecg.modules.weixin.entity.WeixinMessage;
import org.jeecg.modules.weixin.entity.WeixinMessageDetail;
import org.jeecg.modules.weixin.mapper.WeixinMessageDetailMapper;
import org.jeecg.modules.weixin.mapper.WeixinMessageMapper;
import org.jeecg.modules.weixin.service.IWeixinMessageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 微信消息
 * @Author: jeecg-boot
 * @Date:   2019-11-30
 * @Version: V1.0
 */
@Service
public class WeixinMessageServiceImpl extends ServiceImpl<WeixinMessageMapper, WeixinMessage> implements IWeixinMessageService {

	@Autowired
	private WeixinMessageMapper weixinMessageMapper;
	@Autowired
	private WeixinMessageDetailMapper weixinMessageDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(WeixinMessage weixinMessage, List<WeixinMessageDetail> weixinMessageDetailList) {
		weixinMessageMapper.insert(weixinMessage);
		if(weixinMessageDetailList!=null && weixinMessageDetailList.size()>0) {
			for(WeixinMessageDetail entity:weixinMessageDetailList) {
				//外键设置
				entity.setMessageid(weixinMessage.getId());
				weixinMessageDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(WeixinMessage weixinMessage,List<WeixinMessageDetail> weixinMessageDetailList) {
		weixinMessageMapper.updateById(weixinMessage);
		
		//1.先删除子表数据
		weixinMessageDetailMapper.deleteByMainId(weixinMessage.getId());
		
		//2.子表数据重新插入
		if(weixinMessageDetailList!=null && weixinMessageDetailList.size()>0) {
			for(WeixinMessageDetail entity:weixinMessageDetailList) {
				//外键设置
				entity.setMessageid(weixinMessage.getId());
				weixinMessageDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		weixinMessageDetailMapper.deleteByMainId(id);
		weixinMessageMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			weixinMessageDetailMapper.deleteByMainId(id.toString());
			weixinMessageMapper.deleteById(id);
		}
	}
	
}
